<template>
  <!-- 添加角色表单对话框 -->
  <el-form :model="userinfo" label-width="120px" class="search-div">
    <el-form-item label="用户名">
      <el-input v-model="userinfo.userName" />
    </el-form-item>
    <el-form-item label="姓名">
      <el-input v-model="userinfo.name" />
    </el-form-item>
    <el-form-item label="手机号">
      <el-input v-model="userinfo.phone" />
    </el-form-item>
    <!-- 文件上传 -->
    <el-form-item label="头像">
      <el-upload
        class="avatar-uploader"
        action="http://localhost:8200/api/minio/fileUpload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :headers="headers"
      >
        <img v-if="userinfo.avatar" :src="userinfo.avatar" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </el-form-item>
    <el-form-item label="描述">
      <el-input v-model="userinfo.description" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submit">提交</el-button>
      <!-- <el-button @click="dialogVisible = false">取消</el-button> -->
    </el-form-item>
  </el-form>
</template>
<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { putUserAPI } from '@/api/user'
import { useApp } from '@/pinia/modules/app'
//==================个人中心============================
import {useAccount} from '@/pinia/modules/account.js'
import router from '@/router'
//=======================================数据回显功能========================
//获取pinia里的userinfo
const userinfo = ref({ ...useAccount().userinfo })

//==================================按钮提交事件===============================================
 // 获取 store 实例
const accountStore = useAccount()
const submit = async () => {
  const {code} = await putUserAPI(userinfo.value)
  if (code === 200) {
    ElMessage.success('修改成功')
    // 重新获取并更新用户信息//更新pinia里面存储当前登入用户数据
    // 重新获取并更新用户信息
    await accountStore.setUserinfo(userinfo.value)

  }
}
///////////////////////////////////////////////////////////////////////////////文件上传
// 从pinia中获取token，在进行文件上传的时候将token设置到请求头中
const headers = {
  token: useApp().authorization.token,
}
// 图像上传成功以后的事件处理函数
const handleAvatarSuccess = (response, uploadFile) => {
  userinfo.value.avatar = response.data
}
</script>

<style scoped>
.search-div {
  margin-bottom: 10px;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.tools-div {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #ebeef5;
  border-radius: 3px;
  background-color: #fff;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
